﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WGMClient.HelperClasses;
using Microsoft.Win32;
using WGMClient.WGMSvc;
using System.IO;

namespace WGMClient.Views
{
    /// <summary>
    /// Interaction logic for ProfileView.xaml
    /// </summary>
    public partial class ProfileView : UserControl, IMessageListener
    {
        bool _imageChanged = false;
        byte[] _imageData;
        public ProfileView()
        {
            InitializeComponent();
            _profileFontColourChooser.SelectedColor = Colors.Black;
            _profileFontSizeChooser.SelectedIndex = 1;
            _profileFontChooser.SelectedIndex = 1;
            WGMClient.HelperClasses.Registry.RegisterListener(this);
            OnClearImage(this, null);
        }
        public void NotifyServerMessage(WGMSvc.WGMMessage message)
        {

        }
        public void NotifyLocalMessage(LocalMessage message) 
        {
            if(message.MessageType == LocalMessageTypes.MY_INFO_CHANGED)
                ResetProfile();
            if(message.MessageType == LocalMessageTypes.MY_IMAGE_CHANGED)
                _imageBorder.Background = new ImageBrush(HelperClasses.HelperFunctions.GetBitmapFromImage(HelperClasses.HelperFunctions.ByteArrayToImage(HelperClasses.Registry.MyInfo.ImageData))) { Stretch = Stretch.Uniform };

        }
        public void ResetAll()
        {
            OnReset(this, null);
        }
        private void ResetProfile()
        {
            try
            {
                _displayNameTB.Text = HelperClasses.Registry.MyInfo.DisplayName;
                _displayStatusTB.Text = HelperClasses.Registry.MyInfo.DisplayStatus;
                _fontBoldChk.IsChecked = HelperClasses.Registry.MyInfo.IsFontBold;
                _fontItalicChk.IsChecked = HelperClasses.Registry.MyInfo.IsFontItalics;
                _profileFontChooser.SelectedItem = HelperClasses.Registry.MyInfo.FontFace;
                _profileFontColourChooser.SelectedColor = HelperClasses.Registry.MyInfo.UserColor;
                _profileFontSizeChooser.SelectedItem = (int)HelperClasses.Registry.MyInfo.FontSize;
                _imageChanged = false;                
            }
            catch { }
            OnFontSelectionChanged(this, null);
        }
        private void OnReset(object sender, MouseButtonEventArgs e)
        {
            ResetProfile();
            _imageBorder.Background = new ImageBrush(HelperClasses.HelperFunctions.GetBitmapFromImage(HelperClasses.HelperFunctions.ByteArrayToImage(HelperClasses.Registry.MyInfo.ImageData))) { Stretch = Stretch.Uniform };
            _imageChanged = false;
        }

        private void OnCommit(object sender, MouseButtonEventArgs e)
        {
            WGMMessage message = new WGMMessage();
            message.SenderLogon = HelperClasses.Registry.Username;
            message.MessageType = MessageTypes.USER_CHANGED_PROFILE;
            WGMMember member = new WGMMember();
            member.DisplayName = _displayNameTB.Text;
            member.DisplayStatus = _displayStatusTB.Text;
            member.FontColourR = _profileFontColourChooser.SelectedColor.R;
            member.FontColourG = _profileFontColourChooser.SelectedColor.G;
            member.FontColourB = _profileFontColourChooser.SelectedColor.B;
            member.Username = HelperClasses.Registry.Username;
            member.IsFontBold = _fontBoldChk.IsChecked.Value;
            member.IsFontItalics = _fontItalicChk.IsChecked.Value;
            member.FontSize = (int)_fontSampleText.FontSize;
            member.FontFace = _fontSampleText.FontFamily.ToString();

            message.Message = member;

            HelperClasses.Registry.Engine.SendMessageToServer(message);

            if (_imageChanged)
            {
                WGMMessage imageMessage = new WGMMessage();
                imageMessage.MessageType = MessageTypes.USER_CHANGED_IMAGE;
                imageMessage.SenderLogon = HelperClasses.Registry.Username;
                imageMessage.Message = _imageData;
                HelperClasses.Registry.Engine.SendMessageToServer(imageMessage);
            }
            _imageChanged = false;
            HelperClasses.Registry.MainWindow.SetDropDownMessage("Profile Updated.", Colors.White, Colors.SlateBlue, 3500);
            
        }

        private void OnFontSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                _fontSampleText.FontSize = Convert.ToDouble(_profileFontSizeChooser.SelectedItem.ToString());
                _fontSampleText.Foreground = new SolidColorBrush(_profileFontColourChooser.SelectedColor);
                _fontSampleText.FontFamily = (FontFamily)_profileFontChooser.SelectedItem;
                _fontSampleText.FontWeight = _fontBoldChk.IsChecked.Value ? FontWeights.Bold : FontWeights.Normal;
                _fontSampleText.FontStyle = _fontItalicChk.IsChecked.Value ? FontStyles.Italic : FontStyles.Normal;
            }
            catch { }

        }

        private void OnFontOptionChk(object sender, RoutedEventArgs e)
        {
            OnFontSelectionChanged(sender, null);
        }

        private void OnFontColourChanged(Color obj)
        {
            _profileFontColourChooser.SelectedColor = obj;
            OnFontSelectionChanged(null, null);
        }

        private void OnImageClick(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == true)
            {
                FileInfo fi = new FileInfo(ofd.FileName);
                if (fi.Length > (1024 * 1024 * 2))
                {
                    MessageBox.Show("Please select an image file smaller than 2 MB", "WGM Send File Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                try
                {
                    var image = System.Drawing.Image.FromFile(ofd.FileName);
                    _imageBorder.Background = new ImageBrush(HelperClasses.HelperFunctions.GetBitmapFromImage(image)) { Stretch = Stretch.Uniform };
                    _imageData = HelperFunctions.ImageToByteArray(image, image.RawFormat);
                    _imageChanged = true;
                }
                catch { }
            }

        }

        private void OnClearImage(object sender, MouseButtonEventArgs e)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(System.Windows.Forms.Application.StartupPath + "\\Images\\blank2.png");
            _imageBorder.Background = new ImageBrush(HelperClasses.HelperFunctions.GetBitmapFromImage(img)) { Stretch = Stretch.Uniform };
            _imageData = null;
            _imageChanged = true;
        }
    }
}
